package com.googlecode.scenemaker3d.services
{
	import com.googlecode.scenemaker3d.events.MediaEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.net.FileFilter;
	
	[Event(name="getMedia",type="events.MediaEvent")]
	
	public class GetMedia extends EventDispatcher
	{
		private var file:File = new File();
		private var currentType:String;
		
		public function GetMedia()
		{
		}
		public function locate(type:String):void
		{
			//Should enable flexibility in retrieving media from Internet or HD
			var ext:FileFilter;
			currentType = type;
			
			switch(type)
			{
				case "swf":
					ext = new FileFilter("Media .swf", "*.swf");
					break;
				case "gif":
					ext = new FileFilter("Media .gif", "*.gif");
					break;
				case "jpg":
					ext = new FileFilter("Media .jpg .jpeg", "*.jpg; *.jpeg");
					break;
				case "png":
					ext = new FileFilter("Media .png", "*.png");
					break;
				case "mp3":
					ext = new FileFilter("Media .mp3", "*.mp3");
					break;
				case "flv":
					ext = new FileFilter("Media .flv", "*.flv");
					break;
				case "all":
					ext = new FileFilter("Media .swf .gif .jpeg .jpg .png .mp3 .flv", "*.swf; *.gif; *.jpeg; *.jpg; *.png; *.mp3; *.flv");
					break;
				default: 
					return;
			}
			
			browseHD(ext);
		}
		private function browseHD(filter:FileFilter):void
		{	
			file = File.documentsDirectory;
			file.browseForOpen("Open",[filter]);
			file.addEventListener(Event.SELECT, onFileSelected);
		}
		
		private function onFileSelected(e:Event):void
		{
			var filePath:String = file.nativePath;
			var ext:Array = filePath.split(".");
			
			var result:String = ext[ext.length-1];
			result.toLowerCase();
			
			var mediaInfo:Object = new Object();
			mediaInfo.filePath = filePath;
			mediaInfo.type = result;
			
			this.dispatchEvent(new MediaEvent(MediaEvent.GET_MEDIA, mediaInfo));
		}
	}
}